//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.LoginPageController');
	com.yonyou.ncmm_pac_pda.LoginPageController = function() {
		com.yonyou.ncmm_pac_pda.LoginPageController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$LoginPageController$initialize() {
		var username = $cache.read("username") ? $cache.read("username") : "";
		var password = $cache.read("password") ? $cache.read("password") : "";

		var settinginfo = {
			"username": username,
			"password": password
		};

		$ctx.push(settinginfo);
	}

	function com$yonyou$ncmm_pac_pda$LoginPageController$evaljs(js) {
		eval(js)
	}

	function btnlogin_onclick2(sender, args) {

		$ctx.dataCollect();
		var host = $cache.read('host');
		var port = $cache.read('port');

		if(!host || !port) {
			$toast('请先设置NC服务器信息');
			return;
		}

		var username = $id('txtusername').get('value');
		var userpwd = $id('txtpassword').get('value');
		if(!username) {
			$toast('用户名不能为空');
			return;
		}
		if(!userpwd) {
			$toast('密码不能为空');
			return;
		}

		$cache.write("username", username);
		$cache.write("password", userpwd);

		var state = $app.getNetworkState() || {};
		if(state.available != 'true') {
			$js.toast("网络连接错误！");
			return;
		}
		$app.checkNetWorkReachable(host, port, function(param, args) {
			$js.hideLoadingBar();
			if(args.is_reachable != 'true') {
				$js.toast("服务器地址无法连通！");
				return;
			}

		});

		$js.showLoadingBar({
			msg: "正在登录，请稍后..."
		});
		/**
		 * @{link AppService.js}
		 */
		$app.login(username, userpwd, function(caller, args) {
			//			$js.hideLoadingBar();
			args = args || {};
			var login_result = args.login_result || {};
			if(!!login_result && typeof(login_result) == 'string') {
				login_result = $stringToJSON(login_result);
			}
			if(parseInt(login_result.code) != 1000) {
				$js.toast(login_result.msg);
				return;
			}
			var response = login_result.response || '登录失败';
			if(typeof(response) == 'string' && JSON.isJSONString(response)) {
				response = $stringToJSON(response);
			}
			if(typeof(response) == 'object') {
				var retString = response['return'] || {};
				var returnCode = retString.returnCode || -1;

				if(parseInt(returnCode) == 1111) {
					loginCallBack();
					//					$js.toast('登录成功！');
				} else {
					$js.hideLoadingBar();
					var desc = '登录' + (retString.returnDesc || "失败");
					$js.toast(desc);
				}
			} else {
				$js.hideLoadingBar();
				$js.toast(response);
			}

		}, function(caller, args) {
			$js.hideLoadingBar();
			args = args || {};
			var login_result = args.login_result || {};
			if(!!login_result && typeof(login_result) == 'string') {
				login_result = $stringToJSON(login_result);
			}
			if(parseInt(login_result.code) != 1000) {
				$js.toast(login_result.msg);
			} else {
				$js.toast('未知错误，登录失败！');
			}
		});

	}

	function loginCallBack() {
		//登录成功后，初始化对照表
		NCMMDBUtil.initMappingTable();
		window.uploadTotal = 0;
		window.uploadSum = 0;
		window.uploadSum = window.uploadSum + 1;
		NCMMDBUtil.processDocDataFromNC2017(StockorgVO, "库存组织", {}, [], 0, function() {
			$js.toast('登录成功！');
			$view.open({
				"viewid": "com.yonyou.ncmm_pac_pda.HomePage", //目标页面（首字母大写）全名，
				"isKeep": "false"
			});
		});

	}


	function lblregister_onclick(sender, args) {
		$ctx.dataCollect();
		var host = $cache.read('host');
		var port = $cache.read('port');
		if(!host || !port) {
			$toast('请先设置NC服务器信息');
			return;
		}
		var deviceInfo = $device.getDeviceInfo();
		var jsondevice = $stringToJSON(deviceInfo);
		//var cachedeviceid = $cache.read("deviceuniqueid");
		//if(NCMMPubUtil.isNotEmpty(cachedeviceid)) {
		//	$toast("该设备已注册，不需要重复注册！");
		//	return;
		//}
		var deviceid = jsondevice.deviceid;
		var jsonparam = {
			"user": NCMMHttpUtil.USER,
			"password": NCMMHttpUtil.PASSWORD,
			"exsystemcode": NCMMHttpUtil.EXSYSTEMCODE,
			"modulecode": NCMMHttpUtil.ICMODULECODE,
			"deviceuniqueid": deviceid,
			"devicecode": ""
		};
		//$alert(jsonparam);
		//var jsonparam = {
		//	"user": "fj",
		//	"password": "123456a",
		//	"exsystemcode": "MM",
		//	"deviceuniqueid": deviceid,
		//	"devicecode": ""
		//};
		//调用是否达到最大授权数接口服务
		var checkurl = NCMMHttpUtil.getQuestURL("checkpermission");
		//$alert(checkurl);
		//"http://172.16.4.242:3750/service/checkpermission"
		NCMMHttpUtil.sendHTTPRequest(checkurl, jsonparam, function(data, flag) {
			if(flag > 0) {
				if(flag == 1) { //成功		
					if($isJSONObject(data)) {
						var returnjson = $stringToJSON(data);
						var issuccess = returnjson.issuccess;
						if(issuccess == "0") {
							$toast(returnjson.err_msg);
							return;
						} else if(issuccess == "1") {
							//调用设备注册
							var regurl = NCMMHttpUtil.getQuestURL("deviceregister");
							//"http://172.16.4.242:3750/service/deviceregister"
							NCMMHttpUtil.sendHTTPRequest(regurl, jsonparam, function(data, flag) {
								var returnjson = $stringToJSON(data);
								var issuccess = returnjson.issuccess;
								if(issuccess == "1") {
									$cache.write("devicecode", returnjson.devicecode);
									$cache.write("deviceuniqueid", returnjson.deviceuniqueid);
								}
								$toast(returnjson.ret_msg);
							});
						}
						return;
					} else {
						$alert("服务器连接失败!");
						return;
					}
				} else {
					$alert("服务器连接失败!");
					return;
				}
			} else {
				$alert("服务器连接失败!");
				return;
			}
		});
	}

	function lblloginsetting_onclick2(sender, args) {
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.LoginSettingPage2", //目标页面（首字母大写）全名
			isKeep: "true" //打开新页面的同时是否保留当前页面，true为保留，false为不保留
		});
	}

	function onPageLoad(sender, args) {
		//测试数据

//				$cache.write("host", "219.141.185.73");
//				$cache.write("host", "172.16.4.242");
//				$cache.write("port", "5070");
//				$cache.write("ServerIP", $cache.read("host") + ":" + $cache.read("port"));
//				$id("txtusername").set("value", "ww");
//				$id("txtpassword").set("value", "123456a");

	}
	com.yonyou.ncmm_pac_pda.LoginPageController.prototype = {
		initialize: com$yonyou$ncmm_pac_pda$LoginPageController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$LoginPageController$evaljs
	};
	com.yonyou.ncmm_pac_pda.LoginPageController.registerClass('com.yonyou.ncmm_pac_pda.LoginPageController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}